# -*- coding: utf-8 -*-
import scrapy
import  requests,re,json
from ..items import CaifuItem
from lxml import etree
##  打开头部设置 不然无法运行
class CaiFuSpider(scrapy.Spider):
    name = 'cai_fu'
    biaohao = 0
    allowed_domains = ['eastmoney.com']
    start_urls = ['http://www.eastmoney.com/']
    base_url = "http://55.push2.eastmoney.com/api/qt/clist/get?cb=jQuery112403397853268748843_1576835626335&pn=1&pz=2102&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&fid=f3&fs=m:116+t:3&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f19,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152%271576835626336"
    def parse(self, response):
        url = self.base_url
        req = requests.get(url)
        req.encoding = req.apparent_encoding
        html = req.text
        aa = re.compile("(\[{.+?}\])")
        result = aa.findall(html)
        aaa = json.loads(result[0])
        for i in aaa:
            c = i['f12']
            uurl = "http://datainterface.eastmoney.com/EM_DataCenter/JS.aspx?type=GJZB&sty=HKF10&code={}&js=var%20CompanyInfo=[(x)]&_=1577415032126".format(c)
            yield scrapy.Request(url=uurl,callback=self.page_parse)
    def page_parse(self,response):
        item = CaifuItem()
        uurl = response.url
        print(uurl)
        req = requests.get(uurl)
        req.encoding = req.apparent_encoding
        html = req.text
        aa = re.compile("(\[{.+?}\])")
        result = aa.findall(html)
        aaa = json.loads(result[0])
        bb = aaa[0]['SECURITYCODE'][:-3]#名称
        #c = [bb,aaa[0]['INDUSTRY'],aaa[0]['COMPAE']]
          #  yield scrapy.Request(url=uurl, callback=self.page_parse)
        item['bb'] = bb
        item['INDUSTRY'] = aaa[0]['INDUSTRY']
        item['COMPAE'] = aaa[0]['COMPANYNAME']
        item['PERATIO'] = aaa[0]['PERATIO']
        yield item



